package com.chen.demo.simple.domain.service;

import com.chen.demo.simple.command.BookAddCommand;
import com.chen.demo.simple.command.BookDeleteCommand;
import com.chen.demo.simple.command.BookUpdateCommand;
import com.chen.demo.simple.domain.entity.Book;
import com.chen.demo.simple.repository.BookRepository;
import com.chen.winnerframework.http.response.Response;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class BookService {

    private final BookRepository bookRepository;

    public Response saveBook(BookAddCommand command) {
        Book book = command.to(Book.class);
        bookRepository.insertBook(book);
        return Response.success();
    }


    public Response removeBook(BookDeleteCommand command) {
        Integer id = command.getId();
        bookRepository.deleteBookById(id);
        return Response.success();
    }

    public Response renewBook(BookUpdateCommand command) {
        Book book = command.to(Book.class);
        bookRepository.updateBookById(book);
        return Response.success();
    }

}
